package views.createViews;

import javax.swing.JFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JTextField;
import javax.swing.JComboBox;

import controllers.createControllers.ToevoegenBetalingsGegController;
import core.logic.services.utilServices.ValidatieService;
import core.models.dataModels.createModels.ToevoegenBetalingsGegModel;

import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;

public class ToevoegenBetaalGegPassagier extends JFrame {
	private static final long serialVersionUID = 1L;
	private JTextField txtKaartNummer;
	private JTextField txtVervalMaand;
	private JTextField txtVervalJaar;
	private JTextField txtCvsCode;

	private ToevoegenBetalingsGegModel model;

	public ToevoegenBetaalGegPassagier(ToevoegenBetalingsGegModel modelIn) {

		this.model = modelIn;

		// InformatieDisplay bovenaan scherm
		String prijs = null;
		String zitplaatsType = null;
		if (modelIn.getVluchtType().equals("E")) {
			prijs = Double.toString(model.getVlucht().getPrijsEcon());
			zitplaatsType = "Economy";
		} else {
			prijs = Double.toString(model.getVlucht().getPrijsBusiness());
			zitplaatsType = "Business";
		}

		String infoPane = "<html> " + model.getVlucht().getVluchtNr() + "<br>"
				+ model.getVlucht().getVertrekPlaats() + " - "
				+ model.getVlucht().getAankomstPlaats() + "<br>"
				+ "Zitplaats type: " + zitplaatsType + "<br>" + "Te betalen: "
				+ prijs + " euro " + "</html>";

		JLabel lblPrijsInfoHier = new JLabel(infoPane);

		JLabel lblGelieveUwCreditcardgegevens = new JLabel(
				"Gelieve uw  creditcardgegevens in te voeren");
		lblGelieveUwCreditcardgegevens.setFont(new Font("Tahoma", Font.BOLD
				| Font.ITALIC, 14));

		JLabel lblKaartnummer = new JLabel("Kaartnummer");

		txtKaartNummer = new JTextField();
		txtKaartNummer.setColumns(10);

		JLabel lblVervaldatum = new JLabel("Geldig tot");

		txtVervalMaand = new JTextField();
		txtVervalMaand.setColumns(10);

		txtVervalJaar = new JTextField();
		txtVervalJaar.setColumns(10);

		JLabel lblCvsCode = new JLabel("CVS code");

		txtCvsCode = new JTextField();
		txtCvsCode.setColumns(10);

		JLabel lblKaarttype = new JLabel("KaartType");

		final JComboBox<String> cbKaartTypes = new JComboBox<String>();

		for (String kaartType : modelIn.getCreditCardList()) {
			cbKaartTypes.addItem(kaartType);
		}

		// Controleren of het een updatescherm is indien update worden de velden
		// ingevuld
		if (model.getSchermType() == "update") {
			txtCvsCode.setText(Integer.toString(model.getCvsCode()));
			txtKaartNummer.setText(model.getKaartNummer());
			txtVervalJaar.setText(model.getVervalDatum().substring(2));
			txtVervalMaand.setText(model.getVervalDatum().substring(0, 1));
			cbKaartTypes.setSelectedItem(model.getKaartType());
		}

		JButton btnSubmit = new JButton("Betalen");

		btnSubmit.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {

				Boolean noError = true;

				if (ValidatieService.isPositiveInteger(txtCvsCode, "CVS code")) {
					model.setCvsCode(Integer.parseInt(txtCvsCode.getText()));
				} else {
					noError = false;
				}
				if (ValidatieService.isPresent(txtKaartNummer, "Kaartnummer")) {
					model.setKaartNummer(txtKaartNummer.getText());
				} else {
					noError = false;
				}
				model.setKaartType((String) cbKaartTypes.getSelectedItem());
				if (ValidatieService.isPresent(txtVervalJaar, "Vervaljaar")
						&& ValidatieService.isPresent(txtVervalMaand,
								"Vervalmaand")) {
					model.setVervalDatum(txtVervalMaand.getText() + "/"
							+ txtVervalJaar.getText());
				} else {
					noError = false;
				}

				if (noError) {
					ToevoegenBetalingsGegController.handleView(model);
				}

			}
		});
		
		JLabel label = new JLabel("/");

		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(37)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(btnSubmit)
						.addComponent(lblGelieveUwCreditcardgegevens)
						.addComponent(lblPrijsInfoHier, GroupLayout.PREFERRED_SIZE, 369, GroupLayout.PREFERRED_SIZE)
						.addGroup(groupLayout.createSequentialGroup()
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(lblKaarttype)
								.addComponent(lblKaartnummer)
								.addComponent(lblVervaldatum)
								.addComponent(lblCvsCode))
							.addGap(39)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING, false)
								.addGroup(groupLayout.createSequentialGroup()
									.addComponent(txtVervalMaand, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(label, GroupLayout.PREFERRED_SIZE, 4, GroupLayout.PREFERRED_SIZE)
									.addPreferredGap(ComponentPlacement.UNRELATED)
									.addComponent(txtVervalJaar, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE))
								.addComponent(txtKaartNummer)
								.addComponent(cbKaartTypes, 0, 382, Short.MAX_VALUE)
								.addComponent(txtCvsCode))))
					.addGap(292))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(27)
					.addComponent(lblPrijsInfoHier, GroupLayout.PREFERRED_SIZE, 100, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(lblGelieveUwCreditcardgegevens)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblKaarttype)
						.addComponent(cbKaartTypes, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblKaartnummer)
						.addComponent(txtKaartNummer, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblVervaldatum)
						.addComponent(txtVervalMaand, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(label)
						.addComponent(txtVervalJaar, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblCvsCode)
						.addComponent(txtCvsCode, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(btnSubmit)
					.addContainerGap(249, Short.MAX_VALUE))
		);
		getContentPane().setLayout(groupLayout);
	}
}
